package com.rm2pt.generator.springboot.LabManage.controller;
import com.rm2pt.generator.springboot.LabManage.service.*;
import com.rm2pt.generator.springboot.LabManage.Message.*;
import java.util.Date;
import com.rm2pt.generator.springboot.LabManage.utils.PreconditionException;
import com.rm2pt.generator.springboot.LabManage.utils.StandardOPs;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.RequestMethod;

@RestController
@RequestMapping("/CreateTeamService")
public class CreateTeamServiceController {
	
	//@Autowired HttpSession hs;
	
	@Autowired 
	private CreateTeamService createTeamService;
	
	@RequestMapping(value="/createTeam",method=RequestMethod.POST)	
	public String createTeam(CreateTeamMessage createTeamMsg) {
		
		
		JSONObject object = new JSONObject();
		try {
			object.put("data", createTeamService.createTeam(createTeamMsg.id,createTeamMsg.userId,createTeamMsg.name,createTeamMsg.message));
			object.put("msg", "success");
			object.put("code", "200");
		} catch (Exception e) {
			if (e instanceof PreconditionException) {
				object.put("msg", "PreConditionException");
			}else {
				object.put("msg", "PostConditionException");
				e.printStackTrace();
			}
			object.put("code", "400");
		}
		String s=JSON.toJSONString(object);
		return s;
	}
	@RequestMapping(value="/createGroup",method=RequestMethod.POST)	
	public String createGroup(CreateGroupMessage createGroupMsg) {
		
		
		JSONObject object = new JSONObject();
		try {
			object.put("data", createTeamService.createGroup(createGroupMsg.id,createGroupMsg.teamId,createGroupMsg.name,createGroupMsg.message));
			object.put("msg", "success");
			object.put("code", "200");
		} catch (Exception e) {
			if (e instanceof PreconditionException) {
				object.put("msg", "PreConditionException");
			}else {
				object.put("msg", "PostConditionException");
				e.printStackTrace();
			}
			object.put("code", "400");
		}
		String s=JSON.toJSONString(object);
		return s;
	}
	@RequestMapping(value="/handleTeamApply",method=RequestMethod.GET)	
	public String handleTeamApply(HandleTeamApplyMessage handleTeamApplyMsg) {
		
		
		JSONObject object = new JSONObject();
		try {
			object.put("data", createTeamService.handleTeamApply(handleTeamApplyMsg.teamId,handleTeamApplyMsg.agree));
			object.put("msg", "success");
			object.put("code", "200");
		} catch (Exception e) {
			if (e instanceof PreconditionException) {
				object.put("msg", "PreConditionException");
			}else {
				object.put("msg", "PostConditionException");
				e.printStackTrace();
			}
			object.put("code", "400");
		}
		String s=JSON.toJSONString(object);
		return s;
	}
	@RequestMapping(value="/handleGroupApply",method=RequestMethod.GET)	
	public String handleGroupApply(HandleGroupApplyMessage handleGroupApplyMsg) {
		
		
		JSONObject object = new JSONObject();
		try {
			object.put("data", createTeamService.handleGroupApply(handleGroupApplyMsg.groupId,handleGroupApplyMsg.agree));
			object.put("msg", "success");
			object.put("code", "200");
		} catch (Exception e) {
			if (e instanceof PreconditionException) {
				object.put("msg", "PreConditionException");
			}else {
				object.put("msg", "PostConditionException");
				e.printStackTrace();
			}
			object.put("code", "400");
		}
		String s=JSON.toJSONString(object);
		return s;
	}
	@RequestMapping(value="/dismissGroup",method=RequestMethod.DELETE)	
	public String dismissGroup(DismissGroupMessage dismissGroupMsg) {
		
		
		JSONObject object = new JSONObject();
		try {
			object.put("data", createTeamService.dismissGroup(dismissGroupMsg.groupId));
			object.put("msg", "success");
			object.put("code", "200");
		} catch (Exception e) {
			if (e instanceof PreconditionException) {
				object.put("msg", "PreConditionException");
			}else {
				object.put("msg", "PostConditionException");
				e.printStackTrace();
			}
			object.put("code", "400");
		}
		String s=JSON.toJSONString(object);
		return s;
	}
	@RequestMapping(value="/dismissTeam",method=RequestMethod.DELETE)	
	public String dismissTeam(DismissTeamMessage dismissTeamMsg) {
		
		
		JSONObject object = new JSONObject();
		try {
			object.put("data", createTeamService.dismissTeam(dismissTeamMsg.teamId));
			object.put("msg", "success");
			object.put("code", "200");
		} catch (Exception e) {
			if (e instanceof PreconditionException) {
				object.put("msg", "PreConditionException");
			}else {
				object.put("msg", "PostConditionException");
				e.printStackTrace();
			}
			object.put("code", "400");
		}
		String s=JSON.toJSONString(object);
		return s;
	}
	
}
